package com.iakuil.app.common.core.util;

import cn.hutool.core.collection.CollUtil;
import lombok.experimental.UtilityClass;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/***
 * 集合工具类
 *
 * @author Kai
 */
@UtilityClass
public class CollectionUtils extends CollUtil {

    /**
     * 集合分片
     *
     * @param original  集合
     * @param chunkSize 分片大小
     * @return 分片后的集合
     */
    public static <T> List<Collection<T>> partition(Collection<T> original, int chunkSize) {
        if (original == null) {
            return Collections.emptyList();
        }

        int chunkTotal = (original.size() + chunkSize - 1) / chunkSize;
        return Stream.iterate(0, n -> n + 1)
            .limit(chunkTotal)
            .parallel()
            .map(a -> original.stream().skip((long) a * chunkSize).limit(chunkSize).parallel().collect(Collectors.toList()))
            .collect(Collectors.toList());
    }
}
